﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using Vowei.Core.Models;

namespace Vowei.Core.Models
{
    /// <summary>
    /// 表示一个权限
    /// </summary>
    public class Permission : INamedTable
    {
        public Permission()
        {
            ID = Guid.NewGuid();
        }

        public Guid ID
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public virtual bool Authorized(string permission)
        {
            return string.CompareOrdinal(Title, permission) == 0;
        }


        public int CompareTo(Permission another)
        {
            throw new NotImplementedException();
        }

        public int CompareTo(string permission)
        {
            throw new NotImplementedException();
        }
    }

    public class PermissionComparer : IComparer<Permission>
    {
        public int Compare(Permission x, Permission y)
        {
            return x.ID.CompareTo(y.ID);
        }
    }

    public class PermissionEqualityComparer : IEqualityComparer<Permission>
    {
        public bool Equals(Permission x, Permission y)
        {
            if (x == null && y == null)
                return true;
            else if (x != null && y == null)
                return false;
            else if (x == null && y != null)
                return false;
            else
                return x.ID.CompareTo(y.ID) == 0;
        }

        public int GetHashCode(Permission obj)
        {
            return obj.GetHashCode();
        }
    }


    /// <summary>
    /// 表示一个权限集，用来包含其它权限
    /// </summary>
    public class PermissionSet : Permission, IContainer<Permission>
    {
        public PermissionSet()
        {
            ID = Guid.NewGuid();
            Children = new List<Permission>();
        }
        
        IQueryable ITableItemContainer.Children
        {
            get
            {
                return Children.AsQueryable();
            }
        }

        public virtual List<Permission> Children
        {
            get;
            private set;
        }

        public override bool Authorized(string permission)
        {
            if (base.Authorized(permission))
                return true;
            else
            {
                foreach (var perm in Children)
                {
                    if (perm.Authorized(permission))
                        return true;
                }

                return false;
            }
        }
    }

    /// <summary>
    /// 表示一个用户或者用户组拥有的权限
    /// </summary>
    public class Ace : ITable
    {
        public Ace()
        {
            ID = Guid.NewGuid();
            Permissions = new List<Permission>();
        }

        public Guid ID { get; set; }

        public string RoleName { get; set; }

        public string UserName { get; set; }

        public virtual List<Permission> Permissions { get; private set; }

        public void AddPermission(Permission permission)
        {
            Permissions.Add((Permission)permission);
        }

        public void RemovePermission(Permission permission)
        {
            Permissions.Remove((Permission)permission);
        }
    }
}
